package com.kldy8.kldyw.live;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.kldy8.kldyw.BaseAppCompatActivity;
import com.kldy8.kldyw.R;
import com.kldy8.kldyw.SystemSet;
import com.kldy8.kldyw.find.script.ScriptActivity;
import com.kldy8.kldyw.logre.LoginActivity;
import com.kldy8.kldyw.system.ServerApi;
import com.kldy8.kldyw.system.ServerCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Request;

/**
 * 编辑
 */
public class LiveNameActivity  extends BaseAppCompatActivity
{

    private EditText mLiveNameEditText;
    private Button in_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_live_name);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.live_title));
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true); // 决定左上角图标的右侧是否有向左的小箭头, true
        // 有小箭头，并且图标可以点击
        actionBar.setDisplayShowHomeEnabled(false);


        mLiveNameEditText = (EditText) findViewById(R.id.livenameedittext);
        in_button = (Button) findViewById(R.id.in_button);

        in_button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                String member_id = SystemSet.getInstance().getMember_id();
                final String liveName = mLiveNameEditText.getText().toString();

                if (null == liveName || liveName.equals(""))
                {
                    Toast.makeText(LiveNameActivity.this,"房间名字不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                if (null == member_id || member_id.equals(""))
                {
                    Toast.makeText(LiveNameActivity.this,"直播请先登录账号",Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.setClass(LiveNameActivity.this, LoginActivity.class);
                    startActivityForResult(intent,1001);
                    overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

                }
                else
                {
                    ServerApi.live_add(member_id, liveName, new ServerCallBack()
                    {
                        @Override
                        public void onError(Request request, Exception e)
                        {
                            Toast.makeText(LiveNameActivity.this,"网络错误",Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onResponse(String response) throws JSONException
                        {
                            Log.e("response",response);
                            JSONObject jsonObject = new JSONObject(response);
                            if ("0".equals(jsonObject.optString("code")))
                            {
                                JSONArray objectDataArray = jsonObject.optJSONArray("data");
                                String live_id = null;
                                for (int i = 0; i< objectDataArray.length();i++)
                                {
                                    JSONObject object =  objectDataArray.optJSONObject(i);
                                    live_id = object.optString("live_id");

                                }
                                if (live_id != null)
                                {
                                    Intent intent = new Intent();
                                    intent.setClass(LiveNameActivity.this, com.underconverbg.livedemo.LiveAnchorActivity.class);
                                    intent.putExtra("live_id", live_id);
                                    intent.putExtra("live_name", liveName);
                                    startActivityForResult(intent,0);
                                    overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                                    finish();
                                }
                                else
                                {
                                    Toast.makeText(LiveNameActivity.this,"创建房间失败",Toast.LENGTH_LONG).show();
                                }
                            }
                            else  if ("10003".equals(jsonObject.optString("code")))
                            {
                                Toast.makeText(LiveNameActivity.this,"网络错误",Toast.LENGTH_LONG).show();

                            }
                            else  if ("10002".equals(jsonObject.optString("code")))
                            {
                                Toast.makeText(LiveNameActivity.this,"网络错误",Toast.LENGTH_LONG).show();

                            }
                            else  if ("10004".equals(jsonObject.optString("code")))
                            {
                                Intent intent2 = new Intent();
                                intent2.setClass(LiveNameActivity.this, LoginActivity.class);
                                startActivity(intent2);
                                LiveNameActivity.this.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                            }

                            else
                            {
                                Toast.makeText(LiveNameActivity.this,"网络错误",Toast.LENGTH_LONG).show();
                            }
                        }
                    });
//
                }
            }
        });


    }

    @Override
    protected void onActivityResult( int requestCode, int resultCode, Intent data )
    {
        switch ( resultCode ) {
            case 1002 :
                Intent in = new Intent();
                in.putExtra( "result", "res" );
                int abc = 1002;
                setResult(abc, in );
                finish();
                break;
            default :
                break;
        }

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if (item.getItemId() == android.R.id.home)
        {
            finish();
            return true ;
        }

        return super.onOptionsItemSelected(item);
    }

}
